//
//  UIGestureRecognizer+Block.m
//  yuanjiang
//
//  Created by YuanJiang on 2019/9/19.
//  Copyright © 2019 llbt. All rights reserved.
//

#import "UIGestureRecognizer+Block.h"
#import <objc/runtime.h>

static const int target_key;

@implementation UIGestureRecognizer (Block)

+(instancetype)yj_gesterTargetWithAction:(gesterBlocck)block
{
    __weak typeof(self)weakself = self;
    return [[weakself alloc]initWithBlock:block];
}

- (instancetype) initWithBlock:(gesterBlocck)block
{
    self = [self init];
    [self addActionBlock:block];
    [self addTarget:self action:@selector(ClickAction:)];
    return self;
}

-(void)ClickAction:(id)sender
{
    gesterBlocck block = objc_getAssociatedObject(self, &target_key);
    if (block) {
        block(sender);
    }
}

-(void)addActionBlock:(gesterBlocck)block
{
    if (block) {
        objc_setAssociatedObject(self, &target_key, block, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}

@end

